const jwt = require('jsonwebtoken')
const secret = 'dei2.com'
const tokenExpiresIn = '7d'

module.exports = class extends enkel.controller.base {
  init(http) {
    super.init(http)

    this.RfcModel = this.models('inig/rfc')
    this.PluginModel = this.models('inig/plugin')
    this.UserModel = this.models('inig/user')

    this.Op = this.Sequelize.Op

    this.response.setHeader('Access-Control-Allow-Origin', '*')
    this.response.setHeader('Access-Control-Allow-Headers', '*')
    this.response.setHeader('Access-Control-Allow-Methods', '*')
  }

  getCookie(name) {
    if (!this.request.headers || !this.request.headers.cookies) {
      return !name ? {} : ''
    }
    let _cookies = this.request.headers.cookies.replace(/; /g, ';')
    let outObj = {}
    if (_cookies.trim() === '') {
      if (!name) {
        return {}
      } else {
        return ''
      }
    }
    let _cookiesArr = _cookies.split(';')
    for (let i = 0; i < _cookiesArr.length; i++) {
      if (!outObj.hasOwnProperty(_cookiesArr[i].split('=')[0])) {
        outObj[_cookiesArr[i].split('=')[0]] = _cookiesArr[i].split('=')[1]
      }
    }
    if (!name) {
      return outObj
    } else {
      return outObj[name] || ''
    }
  }

  checkAuth() {
    let enkelCookie = this.getCookie('enkel')
    if (
      !enkelCookie ||
      enkelCookie.trim() !== '9d935f95a1630e1282ae9861f16fcf0b'
    ) {
      return false
    } else {
      return true
    }
  }

  async checkLogin(args) {
    if (!args.token || args.token === '') {
      return false
    }
    let _status = jwt.verify(args.token, secret, (err, decoded) => {
      return err || {}
    })
    if (_status.name === 'TokenExpiredError') {
      return false
    } else {
      let loginUser = await this.UserModel.findOne({
        where: { username: args.username },
      })
      if (!loginUser) {
        loginUser = await this.UserModel.findOne({
          where: { phonenum: args.username },
        })
        if (!loginUser) {
          return false
        } else {
        }
      } else {
      }
      if (loginUser.token === '') {
        return false
      } else {
        let _storeTokenStatus = jwt.verify(
          args.token,
          secret,
          (err, decoded) => {
            return err || {}
          }
        )
        if (_storeTokenStatus.name === 'TokenExpiredError') {
          return false
        } else {
        }
        return true
      }
    }
  }

  async add() {
    this.RfcModel.create({
      pid: 'iJ-dhYmLf7',
      category: '406c3068-6b4d-4863-b040-4be6fba837b4',
      pluginName: '剪切板',
      description: '剪切板管理',
      size: '1623984',
      version: '1.1.0',
      logo: 'http://img.dei2.com/18000000001/logo.png?Fv3h3_9GrUT8F545TSdP9UTgDvVx',
      phonenum: '18000000001',
      main: 'index.html',
      features: {
        searchable: {
          cmds: ['Clipboard', '剪切板', '剪贴板', '剪切板管理', '剪贴板管理'],
        },
        filterable: true,
        singleWindow: true,
      },
      windowOptions: { width: 350, height: 600 },
      author: 'BBTools',
      homepage: 'https://bbt.dei2.com',
      url: 'http://img.dei2.com/18000000001/36ff9043-8f15-43f2-ab69-af978e4d36f8.bbt?Fhx7sM8o83Q5Xf-lSJD-OdmkS5B4',
      status: 1,
    })

    this.RfcModel.create({
      pid: 'uYbSDv4mrI',
      category: '406c3068-6b4d-4863-b040-4be6fba837b4',
      pluginName: 'json',
      description: '增强型json格式化，url参数解析',
      size: '11271141',
      version: '1.0.1',
      logo: 'http://img.dei2.com/18000000001/logo.png?Ft_DWGx14k7jM4tTRduZ3tlfFk-c',
      phonenum: '18000000001',
      main: 'index.html',
      features: {
        searchable: { cmds: ['json', 'json格式化', 'url参数解析'] },
        filterable: true,
      },
      windowOptions: { width: 1000, height: 800 },
      author: 'BBTools',
      homepage: 'https://bbt.dei2.com',
      url: 'http://img.dei2.com/18000000001/uYbSDv4mrI.bbt?lpoP7hCINM1lDPJYLQad9AErNrfw',
      status: 1,
    })

    this.RfcModel.create({
      pid: 'jVzCL7TqCq',
      category: '406c3068-6b4d-4863-b040-4be6fba837b4',
      pluginName: '二维码',
      description: '二维码工具，生成二维码，二维码解析',
      size: '2543929',
      version: '1.0.28',
      logo: 'http://img.dei2.com/18000000001/logo.png?FoysiTNMo7M2rmk7ZP02FDgz0o1w',
      phonenum: '18000000001',
      main: 'index.html',
      features: {
        searchable: { cmds: ['qrcode', '二维码'] },
        filterable: false,
      },
      windowOptions: { width: 1000, height: 800, customTraffic: true },
      author: 'BBTools',
      homepage: 'https://bbt.dei2.com',
      url: 'http://img.dei2.com/18000000001/a911ac3f-4332-4cf5-9fac-eca26b3319da.bbt?FmTmu3-ajxcVyNQpqDzTbj8Lk-ey',
      status: 1,
    })

    this.RfcModel.create({
      pid: 'Nue7JVmKMn',
      category: '406c3068-6b4d-4863-b040-4be6fba837b4',
      pluginName: '内网穿透',
      description: '内网穿透工具',
      size: '1084585',
      version: '1.0.0',
      logo: 'http://img.dei2.com/18000000001/logo.png?Fhg1L9_v_teakkU4W9GMgiPrvkos',
      phonenum: '18000000001',
      main: 'index.html',
      features: {
        searchable: { cmds: ['Intranet', '内网穿透'] },
        filterable: true,
        singleWindow: true,
      },
      windowOptions: { width: 800, height: 600 },
      author: 'BBTools',
      homepage: 'https://bbt.dei2.com',
      url: 'http://img.dei2.com/18000000001/ffe89ad6-3b08-49a9-9982-3c3ea2fc987e.bbt?FsH6uXWRGsywjuj_qjqy3gJpZfFP',
      status: 1,
    })

    this.RfcModel.create({
      pid: 'qjem6tyDkd',
      category: '406c3068-6b4d-4863-b040-4be6fba837b4',
      pluginName: 'video',
      description: '电视直播',
      size: '1367945',
      version: '1.0.3',
      logo: 'http://img.dei2.com/18000000001/logo.png?Fvpxy0FdW3NjvOuKTtzAZLfs46pu&s=XW5eyrsLCdjQfbF',
      phonenum: '18000000001',
      main: 'index.html',
      features: {
        searchable: { cmds: ['video', '电视直播'] },
        filterable: true,
      },
      windowOptions: { width: 800, height: 600 },
      author: 'BBTools',
      homepage: 'https://bbt.dei2.com',
      url: 'http://img.dei2.com/18000000001/d6d88c2b-89bf-4192-9698-1e15469ad876.bbt?FhhYPg2T95Sk2aYmZo34eCWmuEUv',
      status: 1,
    })
    return this.json({
      message: '成功',
      status: 200,
    })
  }

  /**
   * 组件发布
   * @returns
   */
  async publishAction() {
    if (!this.isPost()) {
      return this.json({ status: 405, message: '请求方法不正确', data: {} })
    }
    let params = await this.post()
    let queryResponse = await this.RfcModel.findOne({
      where: { pid: params.pid, status: 0 },
      attributes: { exclude: ['createAt', 'updateAt'] },
    })
    let queryPluginResponse = await this.PluginModel.findOne({
      where: {
        pid: params.pid,
      },
    })
    let _category = ''
    if (queryPluginResponse) {
      _category = queryPluginResponse.dataValues.category
    }
    if (!queryResponse) {
      // 当前插件无审核中
      await this.RfcModel.create({
        pid: params.pid,
        pluginName: params.pluginName,
        version: params.version,
        description: params.description,
        logo: params.logo,
        author: params.author,
        phonenum: params.phonenum,
        homepage: params.homepage,
        main: params.main || 'index.html',
        features: params.features || {},
        windowOptions: params.windowOptions,
        url: params.url || '',
        size: params.size || '0',
        category: _category,
      })
    } else {
      // 当前插件 存在审核中的版本
      if (!params.override || params.override == 'false') {
        return this.json({
          status: 409,
          message: '已经存在审核中的版本',
          data: {},
        })
      } else {
        // 更新 审核中版本的信息
        await this.RfcModel.update(
          {
            pluginName: params.pluginName,
            version: params.version,
            description: params.description,
            logo: params.logo,
            author: params.author,
            phonenum: params.phonenum,
            homepage: params.homepage,
            main: params.main || 'index.html',
            features: params.features || {},
            windowOptions: params.windowOptions,
            url: params.url || '',
            size: params.size || '0',
            category: _category,
          },
          {
            where: {
              pid: params.pid,
            },
          }
        )
      }
    }
    let res = await this.RfcModel.findOne({
      where: { pid: params.pid },
    })
    return this.json({
      status: 200,
      data: res.dataValues || {},
    })
  }

  async listAction() {
    if (!this.isPost()) {
      return this.json({ status: 405, message: '请求方法不正确', data: {} })
    }
    let params = await this.post()
    if (
      !params.token ||
      params.token === '' ||
      !params.phonenum ||
      params.phonenum === ''
    ) {
      return this.json({
        status: 401,
        message: '缺少参数',
        data: { needLogin: true },
      })
    }
    if (!this.checkLogin({ username: params.phonenum, token: params.token })) {
      return this.json({
        status: 401,
        message: '登录状态失效，请重新登录',
        data: { needLogin: true },
      })
    } else {
      let userInfo = await this.UserModel.findOne({
        where: {
          phonenum: params.phonenum,
        },
        attributes: { exclude: ['id', 'password', 'createAt', 'updateAt'] },
      })
      if (!userInfo) {
        return this.json({
          status: 402,
          message: '账号不存在',
          data: {
            needRegister: true,
          },
        })
      } else if (userInfo.role != '0d883eff-d6ae-4992-9f11-848f8aa9c61c') {
        return this.json({
          status: 1001,
          message: '权限不足',
          data: {},
        })
      } else {
        let queryResponse = await this.RfcModel.findAll({
          where: {
            status: Number(params.status) || 0,
          },
          order: [['updatedAt', 'DESC']],
        })
        return this.json({
          status: 200,
          message: '成功',
          data: {
            list: queryResponse || [],
          },
        })
      }
    }
  }

  async listMyPluginsAction() {
    if (!this.isPost()) {
      return this.json({ status: 405, message: '请求方法不正确', data: {} })
    }
    let params = await this.post()
    if (
      !params.token ||
      params.token === '' ||
      !params.phonenum ||
      params.phonenum === ''
    ) {
      return this.json({
        status: 401,
        message: '缺少参数',
        data: { needLogin: true },
      })
    }
    if (!this.checkLogin({ username: params.phonenum, token: params.token })) {
      return this.json({
        status: 401,
        message: '登录状态失效，请重新登录',
        data: { needLogin: true },
      })
    } else {
      let userInfo = await this.UserModel.findOne({
        where: {
          phonenum: params.phonenum,
        },
        attributes: { exclude: ['id', 'password', 'createAt', 'updateAt'] },
      })
      if (!userInfo) {
        return this.json({
          status: 402,
          message: '账号不存在',
          data: {
            needRegister: true,
          },
        })
      } else {
        let queryResponse = await this.RfcModel.findAll({
          where: {
            phonenum: params.phonenum,
          },
          order: [['updatedAt', 'DESC']],
        })
        return this.json({
          status: 200,
          message: '成功',
          data: {
            list: queryResponse || [],
          },
        })
      }
    }
  }

  async reviewAction() {
    if (!this.isPost()) {
      return this.json({ status: 405, message: '请求方法不正确', data: {} })
    }
    let params = await this.post()
    if (
      !params.token ||
      params.token === '' ||
      !params.phonenum ||
      params.phonenum === ''
    ) {
      return this.json({
        status: 401,
        message: '缺少参数',
        data: { needLogin: true },
      })
    }
    if (!params.status || params.status == 0 || params.status == '0') {
      return this.json({
        status: 1001,
        message: '请设置插件的审核状态',
        data: {},
      })
    }
    if (!params.pid) {
      return this.json({
        status: 1001,
        message: '缺少参数',
        data: {},
      })
    }
    if (!this.checkLogin({ username: params.phonenum, token: params.token })) {
      return this.json({
        status: 401,
        message: '登录状态失效，请重新登录',
        data: { needLogin: true },
      })
    } else {
      let userInfo = await this.UserModel.findOne({
        where: {
          phonenum: params.phonenum,
        },
        attributes: { exclude: ['id', 'password', 'createAt', 'updateAt'] },
      })
      if (!userInfo) {
        return this.json({
          status: 402,
          message: '账号不存在',
          data: {
            needRegister: true,
          },
        })
      } else if (userInfo.role != '0d883eff-d6ae-4992-9f11-848f8aa9c61c') {
        return this.json({
          status: 1001,
          message: '权限不足',
          data: {},
        })
      } else {
        let updateParams = {}
        if (params.category) {
          updateParams.category = params.category
        }
        if (params.main) {
          updateParams.main = params.main
        }
        if (params.features) {
          updateParams.features = params.features
        }
        if (params.windowOptions) {
          updateParams.windowOptions = params.windowOptions
        }
        if (params.pluginName) {
          updateParams.pluginName = params.pluginName
        }
        if (params.description) {
          updateParams.description = params.description
        }
        if (params.size) {
          updateParams.size = params.size
        }
        if (params.version) {
          updateParams.version = params.version
        }
        if (params.logo) {
          updateParams.logo = params.logo
        }
        if (params.author) {
          updateParams.author = params.author
        }
        if (params.homepage) {
          updateParams.homepage = params.homepage
        }
        if (params.url) {
          updateParams.url = params.url
        }
        if (params.errmsg) {
          updateParams.errmsg = params.errmsg
        }
        if (params.status) {
          updateParams.status = params.status
        }

        let pluginQueryResponse = await this.PluginModel.findOne({
          where: {
            pid: params.pid,
          },
        })
        enkel.db
          .transaction()
          .then((t) => {
            // 更新 rfc 表
            return this.RfcModel.update(updateParams, {
              where: {
                id: params.id,
              },
              transaction: t,
            })
              .then(async (res) => {
                this.RfcModel.destroy({
                  where: {
                    pid: params.pid,
                    version: {
                      [this.Op.ne]: params.version,
                    },
                  },
                })
                if (params.status == 1) {
                  if (pluginQueryResponse) {
                    // 已经发布过的插件
                    return this.PluginModel.update(updateParams, {
                      where: {
                        pid: params.pid,
                      },
                      transaction: t,
                    })
                  } else {
                    // 未发布过的插件
                    const _plugin = await this.RfcModel.findOne({
                      where: {
                        pid: params.pid,
                      },
                    })
                    return this.PluginModel.create(
                      Object.assign(
                        {},
                        _plugin.dataValues || {},
                        updateParams,
                        {
                          phonenum: params.phonenum,
                          pid: params.pid,
                        }
                      ),
                      {
                        transaction: t,
                      }
                    )
                  }
                }
              })
              .then(() => {
                return t.commit()
              })
              .catch(() => {
                return t.rollback()
              })
              .then(async () => {
                if (params.status == 1) {
                  let count = await this.PluginModel.count({
                    where: {
                      pid: params.pid,
                    },
                  })
                  if (count < 1) {
                    return this.json({
                      status: 1001,
                      message: '失败2',
                      data: {},
                    })
                  } else {
                    return this.json({
                      status: 200,
                      message: '成功',
                      data: params || {},
                    })
                  }
                } else {
                  return this.json({
                    status: 200,
                    message: '成功',
                    data: params || {},
                  })
                }
              })
          })
          .catch((err) => {
            return this.json({ status: 1002, message: '保存失败', data: {} })
          })
      }

      //   let updateResponse = await this.RfcModel.update(updateParams, {
      //     where: {
      //       id: params.id
      //     },
      //   })
      //   console.log('审核结果: ', updateResponse)
      //   if (updateParams[0] > 0) {
      //     let queryPlugin = await this.PluginModel.findOne({
      //       where: {
      //         pid: params.pid
      //       }
      //     })
      //     if (queryPlugin) {
      //       // 已经发布过的插件
      //       let pluginUpdateResponse = await this.PluginModel.update(updateParams, {
      //         where: {
      //           pid: params.pid
      //         }
      //       })
      //       if (pluginUpdateResponse[0] > 0) {
      //         return this.json({
      //           status: 200,
      //           message: '成功',
      //           data: params || {}
      //         })
      //       }
      //     }

      //   } else {
      //     return this.json({
      //       status: 1001,
      //       message: '审核失败，请稍后再试',
      //       data: params || {}
      //     })
      //   }
      // }
    }
  }
  async getRfcInfoAction() {
    if (!this.isPost()) {
      return this.json({ status: 405, message: '请求方法不正确', data: {} })
    }
    let params = await this.post()
    if (
      !params.token ||
      params.token === '' ||
      !params.phonenum ||
      params.phonenum === ''
    ) {
      return this.json({
        status: 401,
        message: '缺少参数',
        data: { needLogin: true },
      })
    }
    if (!this.checkLogin({ username: params.phonenum, token: params.token })) {
      return this.json({
        status: 401,
        message: '登录状态失效，请重新登录',
        data: { needLogin: true },
      })
    } else {
      let userInfo = await this.UserModel.findOne({
        where: {
          phonenum: params.phonenum,
        },
        attributes: { exclude: ['id', 'password', 'createAt', 'updateAt'] },
      })
      if (!userInfo) {
        return this.json({
          status: 402,
          message: '账号不存在',
          data: {
            needRegister: true,
          },
        })
      } else {
        let queryResponse = await this.RfcModel.findOne({
          where: {
            pid: params.pid,
            phonenum: params.phonenum,
          },
          order: [['updatedAt', 'DESC']],
        })
        return this.json({
          status: 200,
          message: '成功',
          data: queryResponse || {},
        })
      }
    }
  }
}
